package com.controller.share;

import com.entity.share.chat.Chat;
import com.service.share.ChatService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class ChatController {
    @Autowired
    private ChatService chatService;

    private String path = System.getProperty("user.dir") + "/static/";

    /* 添加聊天记录
     * file：文件
     *type：聊天内容的类型
     * advisoryID：咨询iD
     * date:聊天的时间
     * content：聊天的内容
     */
    @PostMapping("addChatRecord")
    public Object addChatRecord(HttpServletRequest request, String msg, List<MultipartFile> file) {
        Chat chat = (Chat) JSONObject.toBean(JSONObject.fromObject(msg), Chat.class);
        String doctorID = request.getHeader("TOKEN");
        chat.setUserID(Integer.parseInt(doctorID));
        return chatService.addChatRecord(chat, file);
    }

    /**
     * 聊天记录
     * patientID：讨论患者的ID
     */
    @GetMapping("queryChat")
    public Object queryChat(int advisoryID, HttpServletRequest request) {
        String userID = request.getHeader("TOKEN");
        return chatService.queryChat(advisoryID, Integer.parseInt(userID));
    }

    /**
     * 获取聊天列表
     *
     *
     * @return
     */
    @GetMapping("updateChatTables")
    public Object updateChatTables(HttpServletRequest request){
        HttpSession session = request.getSession();
        String type = (String) session.getAttribute("type");
        String ID = request.getHeader("TOKEN");
        System.out.println(type+ID);
        return chatService.updateChatTables(type,Integer.parseInt(ID));

    }
}
